package com.gsafety.library.Utils;

public class ByteArrayUtil
{

    /**
     * 将短整型的数字，转换成字节数组
     *
     * @param value 短整型数字
     * @return 转换后的数组
     */
    public static byte[] short2ByteArray(short value)
    {
        byte[] array = new byte[Short.SIZE / Byte.SIZE];
        array[0] = (byte) ((value >> 8) & 0xff);
        array[1] = (byte) ((value) & 0xff);
        return array;
    }

    /**
     * 字节数组，转换成短整形
     *
     * @param array 短整型字节数组
     * @return 转换后短整型数字
     */
    public static short byteArray2Short(byte[] array)
    {
        byte[] a = new byte[2];
        int i = a.length - 1;
        int j = array.length - 1;
        for (; i >= 0; i--, j--)
        {
            if (j >= 0)
                a[i] = array[j];
            else
                a[i] = 0;
        }
        int v0 = (a[0] & 0xff) << 8;
        int v1 = (a[1] & 0xff) << 0;
        return (short) (v0 + v1);
    }


    /**
     * 将整型的数字，转换成字节数组
     *
     * @param value 整型数字
     * @return 转换后的字节数组
     */
    public static byte[] int2ByteArray(int value)
    {
        byte[] array = new byte[Integer.SIZE / Byte.SIZE];
        array[0] = (byte) ((value >> 24) & 0xff);
        array[1] = (byte) ((value >> 16) & 0xff);
        array[2] = (byte) ((value >> 8) & 0xff);
        array[3] = (byte) ((value) & 0xff);
        return array;
    }

    /**
     * 将字节数组转换成，整型
     *
     * @param array 字节数组
     * @return 转换后的整型
     */
    public static int byteArray2Int(byte[] array)
    {
        byte[] a = new byte[4];
        int i = a.length - 1;
        int j = array.length - 1;
        for (; i >= 0; i--, j--)
        {
            if (j >= 0)
                a[i] = array[j];
            else
                a[i] = 0;
        }
        int v0 = (a[0] & 0xff) << 24;
        int v1 = (a[1] & 0xff) << 16;
        int v2 = (a[2] & 0xff) << 8;
        int v3 = (a[3] & 0xff) << 0;
        return v0 + v1 + v2 + v3;
    }

    /**
     * 将长整型的数字，转换成字节数组
     *
     * @param value 长整型的数字
     * @return 转化后的数组
     */
    public static byte[] long2ByteArray(long value)
    {
        byte[] array = new byte[Long.SIZE / Byte.SIZE];
        array[0] = (byte) ((value >> 56) & 0xff);
        array[1] = (byte) ((value >> 48) & 0xff);
        array[2] = (byte) ((value >> 40) & 0xff);
        array[3] = (byte) ((value >> 32) & 0xff);
        array[4] = (byte) ((value >> 24) & 0xff);
        array[5] = (byte) ((value >> 16) & 0xff);
        array[6] = (byte) ((value >> 8) & 0xff);
        array[7] = (byte) ((value) & 0xff);
        return array;
    }

    /**
     * 将字节数组转换成，长整型
     *
     * @param array 长整型的数组
     * @return 转换后的长整型数字
     */
    public static long byteArray2Long(byte[] array)
    {
        byte[] a = new byte[8];
        int i = a.length - 1;
        int j = array.length - 1;
        for (; i >= 0; i--, j--)
        {
            if (j >= 0)
                a[i] = array[j];
            else
                a[i] = 0;
        }
        int v0 = (a[0] & 0xff) << 56;
        int v1 = (a[1] & 0xff) << 48;
        int v2 = (a[2] & 0xff) << 40;
        int v3 = (a[3] & 0xff) << 32;
        int v4 = (a[4] & 0xff) << 24;
        int v5 = (a[5] & 0xff) << 16;
        int v6 = (a[6] & 0xff) << 8;
        int v7 = (a[7] & 0xff) << 0;
        return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7;
    }

    /**
     * 将字符，转换成字节数组
     *
     * @param value 字符型的数字
     * @return 转换后的数组
     */
    public static byte[] char2ByteArray(char value)
    {
        byte[] array = new byte[Character.SIZE / Byte.SIZE];
        array[0] = (byte) ((value >> 8) & 0xff);
        array[1] = (byte) ((value) & 0xff);
        return array;
    }

    /**
     * 字节数组转换成，字符型
     *
     * @param array 数组
     * @return 转换后的字符型数字
     */
    public static char byteArray2Char(byte[] array)
    {
        byte[] a = new byte[2];
        int i = a.length - 1;
        int j = array.length - 1;
        for (; i >= 0; i--, j--)
        {
            if (j >= 0)
                a[i] = array[j];
            else
                a[i] = 0;
        }
        int v0 = (a[0] & 0xff) << 8;
        int v1 = (a[1] & 0xff) << 0;
        return (char) (v0 + v1);
    }

    /**
     * 将双精度的数字，转换成字节数组
     *
     * @param value 双精度数字
     * @return 转换后的数组
     */
    public static byte[] double2ByteArray(double value)
    {
        byte[] array = new byte[Double.SIZE / Byte.SIZE];
        long l = Double.doubleToLongBits(value);
        for (int i = 0; i < array.length; i++)
        {
            array[i] = Long.valueOf(l).byteValue();
            l = l >> 8;
        }
        return array;
    }

    /**
     * 字节数组转换成，双精度型
     *
     * @param array 双精度的数组
     * @return 转换后的数字
     */
    public static double byteArray2Double(byte[] array)
    {
        long l;
        l = array[0];
        l &= 0xff;
        l |= ((long) array[1] << 8);
        l &= 0xffff;
        l |= ((long) array[2] << 16);
        l &= 0xffffff;
        l |= ((long) array[3] << 24);
        l &= 0xffffffffl;
        l |= ((long) array[4] << 32);
        l &= 0xffffffffffl;
        l |= ((long) array[5] << 40);
        l &= 0xffffffffffffl;
        l |= ((long) array[6] << 48);
        l &= 0xffffffffffffffl;
        l |= ((long) array[7] << 56);
        return Double.longBitsToDouble(l);
    }

    /**
     * 将单精度的数字，转换成字节数组
     *
     * @param value 浮点型数字
     * @return 转换后的数组
     */
    public static byte[] float2ByteArray(float value)
    {
        byte[] array = new byte[Float.SIZE / Byte.SIZE];
        int l = Float.floatToIntBits(value);
        for (int i = 0; i < array.length; i++)
        {
            array[i] = Integer.valueOf(l).byteValue();
            l = l >> 8;
        }
        return array;
    }

    /**
     * 将字节数组，转换到，单精度
     *
     * @param array 浮点型的数组
     * @return 浮点型的数字
     */
    public static float byteArray2Float(byte[] array)
    {
        int l;
        l = array[0];
        l &= 0xff;
        l |= ((long) array[1] << 8);
        l &= 0xffff;
        l |= ((long) array[2] << 16);
        l &= 0xffffff;
        l |= ((long) array[3] << 24);
        return Float.intBitsToFloat(l);
    }
}
